<?php
require_once 'includes/config.php';
require_once 'includes/database.php';
require_once 'includes/attendance_manager.php';

$database = getDatabaseConnection();
$attendanceManager = new AttendanceManager($database);

// 获取用户列表
$users = $attendanceManager->getUsersForSelect();

if (empty($users)) {
    echo "没有用户数据，无法添加测试记录\n";
    exit;
}

echo "找到用户: " . $users[0]['full_name'] . " (ID: " . $users[0]['id'] . ")\n";

// 添加一些测试考勤记录
$testRecords = [
    [
        'user_id' => $users[0]['id'],
        'record_date' => '2025-10-01',
        'status' => 'normal',
        'morning_clock_in' => '08:30',
        'morning_clock_out' => '12:00',
        'afternoon_clock_in' => '14:00',
        'afternoon_clock_out' => '18:30'
    ],
    [
        'user_id' => $users[0]['id'],
        'record_date' => '2025-10-02',
        'status' => 'late',
        'morning_clock_in' => '09:00',
        'morning_clock_out' => '12:00',
        'afternoon_clock_in' => '14:00',
        'afternoon_clock_out' => '18:30'
    ],
    [
        'user_id' => $users[0]['id'],
        'record_date' => '2025-10-03',
        'status' => 'normal',
        'morning_clock_in' => '08:30',
        'morning_clock_out' => '12:00',
        'afternoon_clock_in' => '14:00',
        'afternoon_clock_out' => '18:30'
    ],
    [
        'user_id' => $users[0]['id'],
        'record_date' => '2025-10-04',
        'status' => 'business_trip',
        'all_day_work_hours' => 8.0
    ],
    [
        'user_id' => $users[0]['id'],
        'record_date' => '2025-10-05',
        'status' => 'leave',
        'notes' => '病假'
    ],
    [
        'user_id' => $users[0]['id'],
        'record_date' => '2025-10-06',
        'status' => 'normal',
        'morning_clock_in' => '08:30',
        'morning_clock_out' => '12:00',
        'afternoon_clock_in' => '14:00',
        'afternoon_clock_out' => '18:30'
    ],
    [
        'user_id' => $users[0]['id'],
        'record_date' => '2025-10-07',
        'status' => 'early_leave',
        'morning_clock_in' => '08:30',
        'morning_clock_out' => '12:00',
        'afternoon_clock_in' => '14:00',
        'afternoon_clock_out' => '17:00'
    ],
    [
        'user_id' => $users[0]['id'],
        'record_date' => '2025-10-08',
        'status' => 'normal',
        'morning_clock_in' => '08:30',
        'morning_clock_out' => '12:00',
        'afternoon_clock_in' => '14:00',
        'afternoon_clock_out' => '18:30'
    ],
    [
        'user_id' => $users[0]['id'],
        'record_date' => '2025-10-09',
        'status' => 'overtime',
        'morning_clock_in' => '08:30',
        'morning_clock_out' => '12:00',
        'afternoon_clock_in' => '14:00',
        'afternoon_clock_out' => '20:00'
    ],
    [
        'user_id' => $users[0]['id'],
        'record_date' => '2025-10-10',
        'status' => 'normal',
        'morning_clock_in' => '08:30',
        'morning_clock_out' => '12:00',
        'afternoon_clock_in' => '14:00',
        'afternoon_clock_out' => '18:30'
    ],
    [
        'user_id' => $users[0]['id'],
        'record_date' => '2025-10-11',
        'status' => 'absent',
        'notes' => '无故缺勤'
    ],
    [
        'user_id' => $users[0]['id'],
        'record_date' => '2025-10-12',
        'status' => 'normal',
        'morning_clock_in' => '08:30',
        'morning_clock_out' => '12:00',
        'afternoon_clock_in' => '14:00',
        'afternoon_clock_out' => '18:30'
    ]
];

$added = 0;
foreach ($testRecords as $record) {
    // 检查记录是否已存在
    if (!$attendanceManager->recordExists($record['user_id'], $record['record_date'])) {
        if ($attendanceManager->addAttendanceRecord($record)) {
            $added++;
            echo "添加记录: " . $record['record_date'] . " - " . $record['status'] . "\n";
        } else {
            echo "添加失败: " . $record['record_date'] . "\n";
        }
    } else {
        echo "记录已存在: " . $record['record_date'] . "\n";
    }
}

echo "\n成功添加 " . $added . " 条测试考勤记录\n";

// 验证记录数量
$recordsData = $attendanceManager->getAttendanceRecords(1, 10, []);
echo "当前总记录数: " . $recordsData['total'] . "\n";
echo "总页数: " . $recordsData['total_pages'] . "\n";
echo "每页显示: 10 条\n";

if ($recordsData['total_pages'] > 1) {
    echo "分页按钮应该显示！\n";
} else {
    echo "只有一页，分页按钮不会显示\n";
}
?>
